﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Access.PowerTools.AbstractionLayer;
using Access.PowerTools.Utilities;

namespace Access.PowerTools.TestConsole
{
    [TestFixture]
    public class TestSet_0004_RestoreObjects
    {
        [SetUp]
        public void Setup()
        {
            Access.PowerTools.Settings.Globals.Init();
            Access.PowerTools.Settings.Globals.SimulationModeOn = true;

            Access.PowerTools.Settings.Globals.HostApplication =
                new Access.PowerTools.Simulation.Engine.SimulationCommunicationBridge();
        }

        [TearDown]
        public void TearDown()
        {
        }

        [Test]
        public void Test_001_RestoreSimulatiorFolderExists()
        {
            System.Console.WriteLine(Access.PowerTools.Settings.Globals.RestoredFilesFolder);

            bool ret = System.IO.Directory.Exists(Access.PowerTools.Settings.Globals.RestoredFilesFolder);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        private void clearRestoreFolder()
        {
            FilesAndFolders.ClearFolder(Access.PowerTools.Settings.Globals.RestoredFilesFolder);
        }

        [Test]
        public void Test_002_RestoreDatabase()
        {

            bool ret1 = false;

            clearRestoreFolder();

            AccessApplication app = new AccessApplication(); 
            AccessDatabase database = app.CurrentDatabase;
             
            Access.PowerTools.Database.Restore.Plug_In.RestoreAgent agent =
                new Access.PowerTools.Database.Restore.Plug_In.RestoreAgent(app);

            agent.RestoreDatabase(Access.PowerTools.Settings.Globals.NorthdwindDatabaseSeedFilesFolder);

            string[] files1 = System.IO.Directory.GetFiles(Access.PowerTools.Settings.Globals.NorthdwindDatabaseSeedFilesFolder);
            string[] files2 = System.IO.Directory.GetFiles(Access.PowerTools.Settings.Globals.RestoredFilesFolder);

            foreach (string file1 in files1)
            {
                string fileName1 = System.IO.Path.GetFileName(file1);
                foreach (string file2 in files2)
                {
                    string fileName2 = System.IO.Path.GetFileName(file2);
                    if (fileName1 == fileName2)
                    {
                        string sourceText = System.IO.File.ReadAllText(file1);
                        string copiedText = System.IO.File.ReadAllText(file2);

                        ret1 = (string.Compare(sourceText, copiedText, true) == 0);
                        if (!ret1)
                        {
                            System.Console.WriteLine("'{0}' - invalid copy", fileName1);
                            break;
                        }
                    }
                }
                if (!ret1) break;
            }

            bool ret = ((files1.Length == files2.Length) && ret1);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }
    }
}
